<template>
  <div>
    <div v-if="data.currentRole != 1">无权限</div>
    <div v-else>
      <el-table :data="data.unSignData">
        <el-table-column
          property="name"
          align="center"
          label="单位名称"
          width="200"
        ></el-table-column>

        <el-table-column
          property="requestRole"
          label="注册角色"
          align="center"
          :formatter="roleFormatter"
          width="100"
        ></el-table-column>
        <el-table-column
          property="status"
          label="账号状态"
          :formatter="statusFormatter"
          align="center"
        />

        <el-table-column label="操作" align="center" width="200">
          <template #default="scope">
            <el-button type="success" @click="passSign(scope.row)"
              >启用</el-button
            >
            <el-button type="danger" @click="refuseSign(scope.row)"
              >禁用</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { reactive, onMounted } from "vue";
import {
  func_checkSignUp,
  func_getAddress,
  func_getAllUser,
  func_getUserInfo,
  func_refuseSignUp,
  func_signUp,

  saveOnIpfs,
} from "../../api/etherneum/api";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import {errorMsg, Role2Name, TradeStatus} from "../../config/glossary";
import { ElMessage } from "element-plus";
import { decrypt, encrypt, getKeyPair } from "../../api/etherneum/jsRSA";
import { aesDecrypt } from "../../api/etherneum/jsAES";

export default {
  name: "index",
  components: {},
  setup(props) {
    const store = useStore();
    const router = useRouter();
    const data = reactive({
      roleValue: "",
      indexOptions: [

        {
          value: 2,
          name: "原料商",
        },
        {
          value: 3,
          name: "农户",
        },
        {
          value: 4,
          name: "产品商",
        },
        {
          value: 5,
          name: "零售商",
        },
      ],
      privateData: "",
      name: "",
      phone: "",
      address: "",
      currentRole: "",
      unSignData: [],
      curUserInfo: {},
    });
    const roleFormatter = (row) => {
      return Role2Name[row.requestRole]

    };
    const statusFormatter = (row) => {

      return TradeStatus[row.status];
    };
    const getAddress = async () => {
      const address = await func_getAddress();
      if (address) {
        data.address = address;
        data.curUserInfo = await func_getUserInfo(address);
        console.log("getCurUser=", data.curUserInfo);
        store.commit("app/SET_ADDRESS", address);
        data.currentRole = data.curUserInfo.role;
        store.commit("app/SET_ROLE", data.currentRole);
      }
    };
    const handleSubmit = () => {
      if (data.currentRole != 0) {
        ElMessage.error("该钱包已注册");
        return;
      }
      let ret = func_signUp(
        data.roleValue,
        data.name,
        data.phone,
        data.privateData
      );
      if (ret != 0) {
        console.log(errorMsg["PermissionDenied"]);
      }
    };
    const filePathChange = () => {
      let res = document.getElementById("file");
      data.fileReady = false;
      const reader = new FileReader();
      reader.readAsArrayBuffer(res.files[0]);
      data.uploadFileName = res.files[0].name;
      reader.onload = (e) => {
        data.fileBlob = e.target.result;
        data.fileReady = true;
        console.log("file:", data.fileBlob);
      };
    };

    const fileDescribe = (name, hash) => {
      return "[文件:" + name + " IPFScid=" + hash + "]\n";
    };
    const uploadFile = async () => {
      console.log("start Upload");
      data.uploadFileHash = await saveOnIpfs(data.fileBlob, (hash) => {
        data.uploadFileHash = hash;
        ElMessage.success("上传成功，cid=" + hash);
        data.privateData += fileDescribe(
          data.uploadFileName,
          data.uploadFileHash
        );
      });
    };
    const passSign = (row) => {
      func_checkSignUp(row.userAddress);
    };
    const refuseSign = (row) => {
      func_refuseSignUp(row.userAddress);
    };
    onMounted(() => {
      getAddress().then(async () => {
        if (data.currentRole == 1) data.unSignData = await func_getAllUser();
      });

      ethereum.on("accountsChanged", () => {
        getAddress();
      });
    });
    return {
      data,
      filePathChange,
      handleSubmit,
      uploadFile,
      roleFormatter,
      passSign,
      refuseSign,
      statusFormatter,
    };
  },
};
</script>

<style lang="scss" scoped>
::v-deep.el-table .cell {
  white-space: pre-line;
}
</style>
